--[[
副本回调相关逻辑
接口列表：
onRoomBegin			副本房间开始
onRoomFinish		副本房间完成
onRoomClear			副本房间清除
onHumanEnterCopyScene	玩家进入副本场景回调
onHumanLeaveCopyScene	玩家离开副本场景回调
--]]

local CopyExcel = require("excel.Map").copy 
local CopyW = require("copy.CopyW")
local CopyDefine = require("copy.Define")
local Timer = require("core.Timer")
local TeamLogic = require("team.TeamLogic")
local CopyTeamSignW = require("copy.CopyTeamSignW")
local Human = require("core.Human")
local Msg = require("core.Msg")
local OfflinePkLogic = require("offlinepk.OfflinePkLogicW")


function onRoomBegin(copyID, roomID)	
  CopyW.roomDatas[copyID] = CopyW.roomDatas[copyID] or {}
  if CopyW.roomDatas[copyID][roomID] then
      assert()
  end
  CopyW.roomDatas[copyID][roomID] = { onlineNum = 0,  startTime=Timer.now, quitList= {}} 
end

function onRoomFinish(copyID, roomID, retCode)
  retCode = retCode + 0
	if retCode == CopyDefine.COPY_FINISH_OK and CopyExcel[copyID].type == CopyDefine.COPY_TYPE_TEAM then
--		FriendLogic.onRoomFinishCB(copyID, roomID)
	end
  CopyW.roomDatas[copyID][roomID].finishCode = retCode
  
  if CopyExcel[copyID].type == CopyDefine.COPY_TYPE_FAMILY then
     CopyW.clearFamilyCopy(copyID, roomID, true)      
  end
end

function onRoomClear(copyID, roomID)
  local finishCode = CopyW.roomDatas[copyID][roomID].finishCode
  CopyW.roomDatas[copyID][roomID] = nil
    
  if CopyExcel[copyID].type == CopyDefine.COPY_TYPE_FAMILY then
     CopyW.clearFamilyCopy(copyID, roomID, not finishCode)
  elseif CopyExcel[copyID].type == CopyDefine.COPY_TYPE_TEAM then
     CopyW.clearTeamCopy(copyID, roomID)
  end
  for k,v in pairs(CopyW.copyHuman2RoomCopy) do
      if v.roomID == roomID then
        CopyW.copyHuman2RoomCopy[k] = nil
      end
  end

end

function onHumanBeginCopy(copyID, roomID, uuid)
  CopyW.copyHuman2RoomCopy[uuid] = {}
  CopyW.copyHuman2RoomCopy[uuid].copyID = copyID
  CopyW.copyHuman2RoomCopy[uuid].roomID = roomID
  
  CopyTeamSignW.onBeginCopy(uuid, copyID)
end
function onHumanEnterCopyScene(copyID, roomID, uuid)
  CopyW.roomDatas[copyID][roomID].onlineNum = CopyW.roomDatas[copyID][roomID].onlineNum + 1
  if copyID == CopyDefine.COPY_OFFLINE_PK_ID then
      OfflinePkLogic.onHumanEnterCopy(uuid)
  elseif copyID == CopyDefine.COPY_DHFIGHT_ID1 
      or copyID == CopyDefine.COPY_DHFIGHT_ID2 
      or copyID == CopyDefine.COPY_DHFIGHT_ID3
      or copyID == CopyDefine.COPY_DHFIGHT_ID4
      or copyID == CopyDefine.COPY_DHFIGHT_ID5 then
      local team = TeamLogic.getTeamByUuid(uuid)
      if team then
        TeamLogic.exit(uuid)
      end
  end
end

function onHumanLeaveCopyScene(copyID, roomID, uuid)
  local team = TeamLogic.getTeamByUuid(uuid)
  if team and CopyExcel[copyID].type == CopyDefine.COPY_TYPE_TEAM then
      local leader = TeamLogic.getLeaderUuid(team)  -- 如果是队长，则要把copyTeam2Copy清掉
      if leader == uuid then
          CopyW.clearTeamCopy(copyID, roomID)
      end
  end
  if team and team.count == 1 then
    TeamLogic.exit(uuid)
  end
  if CopyW.roomDatas[copyID][roomID] == nil then
     return
  end 
  CopyW.roomDatas[copyID][roomID].onlineNum = CopyW.roomDatas[copyID][roomID].onlineNum - 1  
  if CopyW.roomDatas[copyID][roomID].onlineNum == 0 then
     CopyW.clearTeamCopy(copyID, roomID)   
  end  
  CopyW.roomDatas[copyID][roomID].quitList[uuid] = 1
end

--组队相关回调
function syncTeamCB(type, copyID, roomID, uuid)
  local msg = Msg.wl.WL_COPY_TEAM_CB
  msg.type = type
  msg.copyID = copyID
  msg.roomID = roomID
  msg.uuid = uuid
  Msg.send2logic(msg, uuid)
end
function onAddMemberBeforeCB(team, uuid)
  local human = Human.onlineUuid[uuid]
  if human == nil then return end  
  CopyTeamSignW.onDelMemberCB(team, uuid)
end
function onAddMemberAfterCB(team, uuid)

end
-- 队伍出人回调
function onDelTeamMemberBeforeCB(team, uuid)
  local human = Human.onlineUuid[uuid]
  if human == nil then return end
  CopyTeamSignW.onDelMemberCB(team, uuid)
end
function onDelTeamMemberAfterCB(team,uuid)
  local human = Human.onlineUuid[uuid]
  if human == nil then return end
  local info = CopyW.copyHuman2RoomCopy[uuid] 
  if info == nil then        
      return
  end  
  if CopyExcel[info.copyID].type ~= CopyDefine.COPY_TYPE_TEAM then
      return
  end
  CopyW.copyHuman2RoomCopy[uuid]  = nil
  local room = CopyW.roomDatas[info.copyID][info.roomID]  
  if not room or team.id ~= room.teamID then
      return
  end
  
  syncTeamCB("onDelMemberCB", info.copyID, info.roomID, uuid)
end

function onTeamDismissCB(team)
  CopyW.copyTeam2Copy[team.id] = nil
end

function onTeamChangeLeaderCB(team, uuid)
  local human = Human.onlineUuid[uuid]
  if human == nil then return end
  CopyTeamSignW.onChangeLeader(team, uuid)
end

--军团相关回调
function syncFamilyCB(type, copyID, roomID, uuid)
  local msg = Msg.wl.WL_COPY_FAMILY_CB
  msg.type = type
  msg.copyID = copyID
  msg.roomID = roomID
  msg.uuid = uuid
  Msg.send2logic(msg, uuid)
end
--离开军团
function onQuitFamilyCB(family, uuid)
  local human = Human.onlineUuid[uuid]
  if human == nil then return end
  local info = CopyW.copyHuman2RoomCopy[uuid] 
  if info == nil then        
      return
  end  
  if CopyExcel[info.copyID].type ~= CopyDefine.COPY_TYPE_FAMILY then
      return
  end
  CopyW.copyHuman2RoomCopy[uuid]  = nil
  local room = CopyW.roomDatas[info.copyID][info.roomID]  
  if not room then
      return
  end
  syncFamilyCB("onQuitFamilyCB", info.copyID, info.roomID, uuid)
end
